package minisoft.application.loterryservice;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class Setting extends Activity {
	private Spinner region;
	private Button btnSubmit;
	//private String string_region;
	//private String string_province;
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		
		//binding region
		region = (Spinner) findViewById(R.id.region);
		region.setOnItemSelectedListener(new CustomOnItemSelectedListener());
		region.setSelection(2);		
		
		addListenerOnButton();
	}
	
	public class CustomOnItemSelectedListener implements OnItemSelectedListener {	  
		private Spinner province;
		private String[] array_province;	 
	
		public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {			
			//province resource	    
			if(pos == 0)
			{	    	
				array_province = view.getResources().getStringArray(R.array.region_north);
				
			}else if(pos == 1)
			{
				array_province = view.getResources().getStringArray(R.array.region_middle);
				   	
			}else if(pos == 2)
			{		
				array_province = view.getResources().getStringArray(R.array.region_south);
			}
			
			// Application of the Array to the province Spinner	
			province = (Spinner) findViewById(R.id.province);	
			ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(Setting.this, android.R.layout.simple_spinner_item, array_province);
			spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
			// The drop down view
			province.setAdapter(spinnerArrayAdapter);
			province.setSelection(5);
			/*
			Toast.makeText(parent.getContext(), 
				"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
				Toast.LENGTH_SHORT).show();
				*/
		}
			 
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
		}
	 
	}
	 
	public void addListenerOnSpinnerItemSelection() {
		region = (Spinner) findViewById(R.id.region);
		region.setOnItemSelectedListener(new CustomOnItemSelectedListener());
	}
	 
	// get the selected dropdown list value
	public void addListenerOnButton() { 
		region = (Spinner) findViewById(R.id.region);
		//Spinner province = (Spinner) findViewById(R.id.province);
		btnSubmit = (Button) findViewById(R.id.btnSubmit);
 
		btnSubmit.setOnClickListener(new OnClickListener() { 
			@Override
			public void onClick(View v) {
				/*update to database
				Toast.makeText(Setting.this, "OnClickListener : " + "\nSpinner 1 : "+ String.valueOf(region.getSelectedItem()) +  
				        "\nSpinner 2 : "+ String.valueOf(province.getSelectedItem()),
					Toast.LENGTH_SHORT).show();
					*/
			} 
		});
	}
}
